(() => {
  // 创建 Vue 实例并挂载到 id 为 main 的元素上
  new Vue({
    el: "#main",
    mixins: [mixin],
    data: {
      // 定义数据
      // 当前登录医生信息
      doctorTestData: {
        docterId: 25,
        doctorName: '赵xx',
        doctorType: "tdoctorInCharge" // tdoctorInCharge-主管医生; dutyDoctor-值班医生
      },
      patientData: {
        patientName: '',
      },
      // 所有的病症列表
      DiseaseData: [{
        diseaseInfoId: "123", //int //病症主键ID
        name: "病症中文名称", // 病症中文名称
        enName: "string //病症英文名称", // 病症英文名称
        createdDate: "date",
        createdBy: "string",
        updatedDate: "date",
        updatedBy: "string",
        value: "", // 病症值
      },], //
      // 新增患者信息
      patientInfo: {
        hospitalInfoId: "2",
        doctorInfoId: "", //int //医生主键【必须】
        doctorName: "",
        patientId: "", //string //患者ID【必须】
        name: "", //string //患者名称【必须】
        gender: "", //string //患者性别【必须】
        age: "", //string //患者年龄【必须】
        admissionTime: "", //date //患者入院时间【必须】
        firstIcuAdmissionTime: "", //date //患者首次ICU入院时间
        degreeOfSepsis: " ", //double //患者确诊患脓毒症的程度(只用传参具体占比就行,不用带%号)
        isDiagnosedWithSepsis: "", //是否确诊为脓毒症 1-是;0-否
        diagnosedTime: "", //date //确诊为脓毒症的时间
        // patientDiseaseCreateList: [
        //   // {
        //   //     "diseaseInfoId":"int //病症主键【必须】",
        //   //     "value":"string //病症值【必须】"
        //   // }
        // ],
        createdBy: "", //string //创建人
        updatedBy: "", //string //更新人
      },
      doctorList: [], // 医生列表
      showPickerVisible: false,
      currentDate: "",
      pickerType: "",
      icuTimePassed: false,
      // timetest:"",
      // timeTest:[{time:""},{time:""},{time:""},{time:""},{time:""},{time:""},{time:""},]
    },
    created () {
      toastr.options = {
        positionClass: 'toast-top-center'
      };
      // 初始化页面 需要获取当前医生信息
      this.initData()
      // 页面加载完成后执行的方法
      // this.getDiseaseData();
      this.getdoctorList()
    },
    methods: {
      // 显示选择器
      showPicker (type) {
        this.maxDate = this.getTodayEnd()
        this.pickerType = type; // 设置选择器类型
        this.currentDate = this.getValueByPath(this, type) ? dayjs(this.getValueByPath(this, type)).toDate() : "";
        this.showPickerVisible = true; // 显示选择器
      },
      onConfirm (value) {
        this.setValueByPath(this, this.pickerType, dayjs(value).format('YYYY-MM-DD HH:mm:ss'), "")
        this.showPickerVisible = false; // 关闭选择器
        this.icuTimePassed = false;
      },
      // 定义方法
      initData () {
        // 获取医生信息
        const doctorData = sessionStorage.getItem("doctorTestData")
        if (doctorData) {
          Object.assign(this.doctorTestData, JSON.parse(doctorData))
          // 更新医院id
          this.patientInfo.hospitalInfoId = this.doctorTestData.hospitalInfoId
          // 更新医生id
          this.patientInfo.doctorInfoId = this.doctorTestData.doctorInfoId
          // 更新医生姓名
          this.patientInfo.doctorName = this.doctorTestData.doctorName
          // 进入新增页更新 医生类型将本地医生类型更新
          this.patientInfo.doctorType = this.doctorTestData.doctorType
        }
      },
      doctorChange () {
        if (this.patientInfo.doctorInfoId) {
          let data = this.doctorList.find((item) => item.doctorInfoId == this.patientInfo.doctorInfoId)
          if (data) {
            this.patientInfo.doctorName = data.name
          }
        }
      },
      // 进入页面查询医生列表  /doctor/list/allGETPOST
      getdoctorList () {
        try {
          const doctorTestData = sessionStorage.getItem("doctorTestData")
          const { hospitalInfoId } = JSON.parse(doctorTestData)
          const URL = "/doctor/list/all?hospitalInfoId=" + hospitalInfoId;
          fetch(apiUrl + URL, {
            method: "POST",
            headers: {
              "Content-Type": "application/json",
            },
            body: JSON.stringify({ hospitalInfoId: hospitalInfoId })
          })
            .then((response) => {
              return response.json();
            })
            .then((data) => {
              if (data.code == "200") {
                this.doctorList = data.data
              } else {
                toastr.error(data.msg);
              }
            })
        } catch (error) {
          console.log(error)
        }
      },
      Patientback () {
        const doctorTestData = sessionStorage.getItem("doctorTestData")
        // 查询当前医生名下患者列表
        if (doctorTestData) {
          const { doctorInfoId, doctorName, doctorType, doctorId, hospitalInfoId } = JSON.parse(doctorTestData)
          window.location.href = "AI_patient_list.html?doctorInfoId=" + doctorInfoId + "&doctorName=" + doctorName + "&doctorType=" + doctorType + "&doctorId=" + doctorId + "&hospitalInfoId=" + hospitalInfoId;
        }
      },
      admissionTimeHander (e) {
        if (this.patientInfo.firstIcuAdmissionTime && e) {
          if (dayjs(e).isAfter(dayjs(this.patientInfo.firstIcuAdmissionTime))) {
            // 判断 首次ICU入院时间 是否在 入院时 之后
            this.patientInfo.admissionTime = ""
            toastr.error('患者入院时间必须在患者首次入住ICU时间之前！');
            return
          }
        }
      },
      firstIcuAdmissionTimeHander (e) {
        if (this.patientInfo.admissionTime && e) {
          if (dayjs(this.patientInfo.admissionTime).isAfter(dayjs(e))) {
            // 判断 首次ICU入院时间 是否在 入院时 之后
            this.patientInfo.firstIcuAdmissionTime = ""
            toastr.error('患者首次入住ICU时间必须在患者入院时间之后！');
            return
          }
        }
      },
      // 提交患者信息 
      addPatient () {
        event.preventDefault()
        if (!this.patientInfo.name) {
          toastr.error('请输入患者名字！');
          return
        } else if (!this.patientInfo.patientId) {
          toastr.error('请输入患者ID！');
          return
        } else if (!this.patientInfo.admissionTime) {
          toastr.error('请输入患者入院时间！');
          return
        } else if (!this.patientInfo.firstIcuAdmissionTime) {
          toastr.error('请输入患者首次入住ICU时间！');
          return
        } else if (dayjs(this.patientInfo.admissionTime).isAfter(dayjs(this.patientInfo.firstIcuAdmissionTime))) {
          // 判断 首次ICU入院时间 是否在 入院时 之后
          toastr.error('患者首次入住ICU时间必须在患者入院时间之后！');
          return
        } else if (dayjs(this.patientInfo.firstIcuAdmissionTime).diff(dayjs(this.patientInfo.admissionTime), 'day') > 15 && !this.icuTimePassed) {
            vant.Dialog.confirm({
              title: '提示',
              message: '患者首次入住ICU时间和患者入院时间之间超过15天！是否继续？',
            })
              .then(() => {
                this.icuTimePassed = true
                this.addPatient()
              })
              .catch(() => {
                this.icuTimePassed = false
              });
            return
        } else if (!this.patientInfo.age) {
          toastr.error('请输入患者年龄！');
          return
        } else if (!this.isPositiveInteger(this.patientInfo.age)) {
          // 判断患者年龄是否为数字
          toastr.error('请输入合法年龄！');
          return
        } else if (this.patientInfo.age > 125 || this.patientInfo.age < 18) {
          // 判断患者年龄是否在 18-125
          toastr.error('患者年龄区间为18-125岁！');
          return
        } else if (!this.patientInfo.gender) {
          toastr.error('请输入患者性别！');
          return
        } else if (!this.patientInfo.doctorInfoId) {
          toastr.error('请选择医生！');
          return
        }
        // 对病症进行循环遍历判断是否存在未选择的如果有做出提示 信息 并拦截
        // 数据都满足要求对保存对象做数据重组 满足接口要求做提交操作
        // this.patientInfo.patientDiseaseCreateList = []
        // this.DiseaseData.map(item => {
        //   this.patientInfo.patientDiseaseCreateList.push({
        //     diseaseInfoId: item.diseaseInfoId,
        //     value: item.value,
        //   })
        // })
        // 校验通过后 调用保存接口做存储
        this.patientInfo.admissionTime = dayjs(this.patientInfo.admissionTime).format('YYYY-MM-DD HH:mm:ss')
        this.patientInfo.firstIcuAdmissionTime = dayjs(this.patientInfo.firstIcuAdmissionTime).format('YYYY-MM-DD HH:mm:ss')
        this.addPatientService(JSON.parse(JSON.stringify(this.patientInfo)))
      },
      // 提交患者信息 /patient/add
      addPatientService (data) {
        // 新增时对患者id 患者姓名做基础加密落库
        let endata = {
          patientId: this.Encryption(data.patientId),
          name: this.Encryption(data.name)
        }
        const URL = "/patient/add";
        fetch(apiUrl + URL, {
          method: "POST",
          headers: {
            "Content-Type": "application/json",
          },
          body: JSON.stringify(Object.assign(data, endata))
        })
          .then((response) => {
            return response.json();
          })
          .then((data) => {
            if (data.code == "200") {
              toastr.success('新增患者成功!');
              // 新增患者成功后跳转页面
              // 拼接参数
              const doctorTestData = sessionStorage.getItem("doctorTestData")
              // 查询当前医生名下患者列表
              if (doctorTestData) {
                const { doctorInfoId, doctorName, doctorType, doctorId } = JSON.parse(doctorTestData)
                window.location.href = "AI_patient_list.html?doctorInfoId=" + doctorInfoId + "&doctorName=" + doctorName + "&doctorType=" + doctorType + "&doctorId=" + doctorId + "&hospitalInfoId=" + hospitalInfoId;
              }
              // const { doctorInfoId, doctorName, doctorType } = this.doctorTestData
              // window.location.href = "AI_patient_list.html?"
            } else if (data.code == "-1") {
              toastr.error(data.msg ? data.msg : "操作失败！");
              // 新增失败对患者姓名 患者id做解码还原处理

            }
          })
          .catch((error) => {
            console.error(error);
          });
      },
      // 获取所有的病症
      getDiseaseData () {
        const URL = "/disease/list";
        fetch(apiUrl + URL, {
          method: "POST",
          headers: {
            "Content-Type": "application/json",
          },
        })
          .then((response) => {
            return response.json();
          })
          .then((data) => {
            console.log(data, "病症列表信息")
            if (data.code === 200) {
              this.DiseaseData = data.data ?? []
              this.DiseaseData = this.DiseaseData.map(item => {
                item.value = "0"
                return item
              })
            } else {
              console.error(data.msg);
            }
          })
          .catch((error) => {
            console.error(error);
          });
      },
    },
  });
})();
